package kaktus.jogl.helloworld;

/**
 * Created with IntelliJ IDEA.
 * User: Admin
 * Date: 07.03.13
 * Time: 2:10
 * To change this template use File | Settings | File Templates.
 */

import com.jogamp.opengl.util.FPSAnimator;

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

class Renderer implements GLEventListener
{
    private GLU glu = new GLU();
    private FPSAnimator animator;
    public float distance=100;
    public float WidthHeightRatio;
    private float angle;
    final int fps = 40;
    final int radius=3;

    public void setAngle(float a){
        angle=a;
    }
    public float getAngle(){
        return angle;
    }

    public void DrawPlayer(GL2 gl, int x, int y, int z, float angle){
        float inc = (float)Math.PI/50;
        gl.glLoadIdentity();
        gl.glTranslatef(-10f, 0.0f, -6.0f);
        gl.glColor4f(0,1,0,1);
        for(float i=0; i<2*Math.PI; i+=inc){
            gl.glBegin(GL2.GL_TRIANGLES);
            gl.glVertex3f(x,y,z);
            gl.glVertex3f((float)(x+Math.cos(i)*radius),(float)(y+Math.sin(i)*radius),z);
            gl.glVertex3f((float)(x+Math.cos(i+inc)*radius),(float)(y+Math.sin(i+inc)*radius),z);
            gl.glEnd();
        }
        gl.glBegin(GL2.GL_LINES);
        gl.glLineWidth(2);
        if (angle != 0){
             gl.glVertex3f(x,y,z) ; gl.glVertex3f((float)(x+Math.cos(angle)*6),
                    (float)(y+Math.sin(angle)*6),z);
        }  /*else {
            gl.glVertex3f(x,y,z); gl.glVertex3f(x+radius,y,z);
        }*/
        gl.glEnd();
    }


    public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged)
    {
    	System.out.println("displayChanged called");
    }

    public void init(GLAutoDrawable gLDrawable)
    {
    	System.out.println("init() called");
        GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL2.GL_SMOOTH);
        animator = new FPSAnimator(gLDrawable,fps);
        animator.start();

    }

    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height)
    {
    	System.out.println("reshape() called: x = "+x+", y = "+y+", width = "+width+", height = "+height);
        final GL2 gl = gLDrawable.getGL().getGL2();

        if (height <= 0) // avoid a divide by zero error!
        {
            height = 1;
        }

       WidthHeightRatio = (float) width / (float) height;

        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, WidthHeightRatio, 1.0, 20.0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }


	public void dispose(GLAutoDrawable arg0)
	{
		System.out.println("dispose() called");
	}

    public void setCamera(GL2 gl, GLU glu, float distance,float ratio){
          System.out.println("setCamera is called");
          gl.glMatrixMode(GL2.GL_PROJECTION);
          gl.glLoadIdentity();
          glu.gluPerspective(45,ratio,1,1000);
          glu.gluLookAt(-distance/3,distance/3,distance,0,0,0,0,1,0);
          gl.glMatrixMode(GL2.GL_MODELVIEW);
          gl.glLoadIdentity();
    }

    public void display(GLAutoDrawable gLDrawable)
    {
        if (!animator.isAnimating()) {
                   return;
               }
        System.out.println("display called");
        System.out.println("distance= "+distance);
        System.out.println("angle= " + angle);
        final GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        setCamera(gl, glu, distance,WidthHeightRatio);
        DrawPlayer(gl,0,0,0,angle);

    }

}